package com.tornado.community.controller;

import com.tornado.community.dto.QuestionDto;
import com.tornado.community.mapper.custome.MyQuestionMapper;
import com.tornado.community.model.Question;
import com.tornado.community.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Objects;

@Controller
public class PublishController {
    private final MyQuestionMapper myQuestionMapper;
    @Autowired
    public PublishController(MyQuestionMapper myQuestionMapper) {
        this.myQuestionMapper = myQuestionMapper;
    }

    @GetMapping("/publish")
    public String publish() {
        return "publish";
    }

    @PostMapping("/publish")
    public String doPublish(@RequestParam("title") String title,
                            @RequestParam("description") String description,
                            @RequestParam("tag") String tag,
                            HttpSession session, Model model) {
        //获取用户
        User user = (User) session.getAttribute("user");
        if (user == null) {
            model.addAttribute("error", "用户未登录");
            return "publish";
        } else {
            //补充问题信息
            Question question = new Question();
            question.setTitle(title);
            question.setDescription(description);
            question.setTag(tag);
            question.setCreator(user.getId());
            question.setGmtCreate(System.currentTimeMillis());
            question.setGmtModify(question.getGmtCreate());
            myQuestionMapper.insert(question);
            return "redirect:/";
        }
    }

    @GetMapping("/publish/{id}")
    public String editPublish(@PathVariable("id") Integer id,
                              Model model) {
        QuestionDto questionDto = Objects.requireNonNull(myQuestionMapper.selectQuestionById(id), "找不到要编辑的问题。");
        model.addAttribute("question", questionDto);
        return "publish_edit";
    }

    @PostMapping("/publish/{id}")
    public String doEditPublish(@PathVariable("id") Integer id,
                                @RequestParam("title") String title,
                                @RequestParam("description") String description,
                                @RequestParam("tag") String tag,
                                HttpSession session, Model model) {
        //获取登陆的用户
        User user = (User) session.getAttribute("user");
        //获取编辑的问题
        QuestionDto questionDto = Objects.requireNonNull(myQuestionMapper.selectQuestionById(id), "找不到要编辑的问题。");
        if (user.getId().intValue() != questionDto.getCreator().intValue()) {
            model.addAttribute("error", "横向越权。");
            return "publish";
        }
        questionDto.setTitle(title);
        questionDto.setDescription(description);
        questionDto.setTag(tag);
        questionDto.setGmtModify(System.currentTimeMillis());
        myQuestionMapper.update(questionDto);
        return "redirect:/";

    }
}
